﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HotShot.Service.Entities;
using HotShot.Entities;

namespace HotShot.Service.BusinessLogic.MessageProcessors
{
    public class InitNodeProcessor: MessageProcessorBase  
    {
        public override void ProcessMessage(HotShot.Entities.Messages.BaseMessage message, string correlationId)
        {
            var descriptor = message.XmlPayload.FromXml<ProcessorNodeDescriptor>();
            if (descriptor.NodeId == ClusterProcessorLogic.Current.ProcessorId)
                return;

            if (ClusterProcessorLogic.Current.GetKnownNodes(true, false).Any(i => i.NodeId == descriptor.NodeId))
            {
                Trace.WriteLine("GCP node already known: " + descriptor.NodeId);
                return;
            }

            ClusterProcessorLogic.Current.SetKnownNodes(new List<ProcessorNodeDescriptor> { descriptor, }, true);

            var known = ClusterProcessorLogic.Current.GetKnownNodes(true, true);
            QueueCommunicationLogic.Current.SendInputMessage(message.ReturnAddress, Messages.KnownNodes(known), correlationId);
        }
    }
}
